package com.example.newappchildrchinese.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeandroid.query.Select;
import com.example.newappchildrchinese.R;
import com.example.newappchildrchinese.mode.ChineseWord;

import java.util.List;


/**
 * Created by Jacky on 2015/11/12.
 */

public class WidgetCourse extends LinearLayout {

    private LinearLayout course_bg;
    private TextView course_title, course_word_1, course_word_2, course_word_3;
    private Long course_id;

    public WidgetCourse(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WidgetCourse(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_course, this);
        course_bg = (LinearLayout) findViewById(R.id.course_bg);
        course_title = (TextView) findViewById(R.id.course_title);
        course_word_1 = (TextView) findViewById(R.id.course_word_1);
        course_word_2 = (TextView) findViewById(R.id.course_word_2);
        course_word_3 = (TextView) findViewById(R.id.course_word_3);
    }

    public void setTitle(String title) {
        course_title.setText(title);
    }

    public void setWord(String word1, String word2, String word3) {
        if (word1 != null) {
            course_word_1.setText(word1);
        }
        if (word2 != null) {
            course_word_2.setText(word2);
        }
        if (word3 != null) {
            course_word_3.setText(word3);
        }
    }

    public void isSel(Boolean isel) {
        if (isel) {
            course_bg.setBackgroundResource(R.drawable.bg_course_sel);
        } else {
            course_bg.setBackgroundResource(R.drawable.bg_course);
        }
    }

    public void setCourseId(Long course_id) {
        this.course_id = course_id;

        //再取三个字
        List<ChineseWord> chineseWordList = new Select().from(ChineseWord.class).where("course_id=?", course_id).limit(3).execute();
        if (chineseWordList.size() > 2) {
            this.setWord(chineseWordList.get(0).getWord_title(), chineseWordList.get(1).getWord_title(), chineseWordList.get(2).getWord_title());
        } else {
            this.setWord("-", "-", "-");
        }
    }

    public Long getCourseId(){
        return course_id;
    }
}